package com.yang.config;

import com.yang.domain.dto.Result;
import com.yang.exception.ServiceException;
import com.yang.limit.exception.LimitException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
/**
 * 全局异常解析器，用来一些没有被自定义处理的服务器异常
 *
 * @author yang
 * @since 2022-10-22
 */
@Slf4j
@RestControllerAdvice
public class WebExceptionAdvice {

    /**
     * 处理Service异常
     */
    @ExceptionHandler(value = ServiceException.class)
    public Result handleServiceException(ServiceException e) {
        log.error(e.toString(), e);
        return Result.fail(e.getMessage());
    }

    /**
     * 处理LimitException异常
     */
    @ExceptionHandler(value = LimitException.class)
    public Result handleServiceException(LimitException e) {
        log.error(e.toString(), e);
        return Result.fail(e.getMessage());
    }

    /**
     * 处理Assert异常
     */
    @ExceptionHandler(value = IllegalArgumentException.class)
    public Result handleIllegalArgumentException(IllegalArgumentException e) {
        log.error(e.toString(), e);
        return Result.fail(e.getMessage());
    }

    /**
     * 处理其他异常
     */
    @ExceptionHandler(value = RuntimeException.class)
    public Result handleRuntimeException(RuntimeException e) {
        log.error(e.toString(), e);
        return Result.fail("服务器异常");
    }
}
